package com.lagou.service;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @author 123
 * @date 2020/5/15 14:56
 * @description
 */
public class RpcDecoder extends MessageToMessageDecoder<ByteBuf> {


    private Class<?> clazz;
    private Serializer serializer;

    public RpcDecoder(Class<?> clazz, Serializer serializer) {
        this.clazz = clazz;
        this.serializer = serializer;
    }

//    protected void decode(ChannelHandlerContext channelHandlerContext, Object o, List list) throws Exception {
//        list.add(serializer.deserialize(clazz, o.toString().getBytes("utf-8")));
//    }
//
//    protected void decode(ChannelHandlerContext channelHandlerContext, Byte aByte, List<Object> list) throws Exception {
//        list.add(serializer.deserialize(clazz, aByte));
//
//    }

    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        String ms=byteBuf.toString(Charset.defaultCharset());
        list.add(serializer.deserialize(clazz,ms.getBytes()));

    }
}
